# class LoginForm(forms.Form):
#     username = forms.CharField(
#         label='用户名',
#         widget=forms.TextInput(
#             attrs={'class': 'form-control', 'placeholder': '请输入用户名'}),
#     )
#
#     password = forms.CharField(
#         label='密码',
#         widget=forms.PasswordInput(
#             render_value=True,
#             attrs={'class': 'form-control', 'placeholder': '请输入密码'}),
#     )
#
#     def cleaned_username(self):
#         """钩子函数"""
#         """验证用户名是否存在"""
#         username = self.cleaned_data.get('username')
#         exists = models.User.objects.filter(username=username).exists()
#         if not exists:
#             self.forms.add_error('username', '用户名不存在')
#         return username
#
#     def clean(self):
#         """全局钩子"""
#         """验证用户名和密码是否正确"""
#         username = self.cleaned_data.get('username')
#         password = self.cleaned_data.get('password')
#         user = authenticate(**self.cleaned_data)
#         if not user:
#             raise forms.ValidationError('Username or Password is incorrect')
#         return super().clean()
